using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.Model.Attendance;
using EzDesk.Apps.ServiceImpl.Attendance.Repositories.Interface;

namespace EzDesk.Apps.ServiceImpl.Attendance.Repositories
{
    public partial class EvaluationModifyHistoryRepository : IEvaluationModifyHistoryRepository
    {
        EzDeskAttendanceDataContext _db;

        public EvaluationModifyHistoryRepository(EzDeskAttendanceDataContext db)
        {
            this._db = db;
        }
		
		#region IRepository<EvaluationModifyHistory> Members

        public Guid Add(EvaluationModifyHistory model, bool saveChanges)
        {
            _db.ez_EvaluationModifyHistories.InsertOnSubmit(
                new ez_EvaluationModifyHistory()
                {
					HistoryId = model.Id,
					UserId = model.UserId,
                    Date = model.Date,
					OperatorId = model.OperatorId,						
					ModifyOn = model.ModifyOn,						
                });
            if (saveChanges)
            {
                this.SaveChanges();
            }
            return model.Id;
        }

        public EvaluationModifyHistory GetById(Guid id)
        {
            EvaluationModifyHistory info = null;
            if (id != null)
            {
                info = _db.ez_EvaluationModifyHistories.Where(p => p.HistoryId == id)
                    .Select(p => new EvaluationModifyHistory()
                    {
						Id = p.HistoryId,

                        UserId = p.UserId,
                        Date = p.Date,
						OperatorId = p.OperatorId,						
						ModifyOn = p.ModifyOn,						
                    })
                    .SingleOrDefault();
            }
            return info;
        }

        public IQueryable<EvaluationModifyHistory> GetAll()
        {
            return _db.ez_EvaluationModifyHistories.Select(
                p => new EvaluationModifyHistory()
                {
					Id = p.HistoryId,

                    UserId = p.UserId,
                    Date = p.Date,
					OperatorId = p.OperatorId,						
					ModifyOn = p.ModifyOn,						
                }
            );
        }

        public void Delete(Guid id, bool saveChanges)
        {
            ez_EvaluationModifyHistory entity = _db.ez_EvaluationModifyHistories.Where(p => p.HistoryId == id).SingleOrDefault();
            if (entity != null)
            {
                _db.ez_EvaluationModifyHistories.DeleteOnSubmit(entity);
            }
            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void Update(EvaluationModifyHistory model, bool saveChanges)
        {
            var entity = _db.ez_EvaluationModifyHistories.Where(p => p.HistoryId == model.Id)
                .SingleOrDefault();

            entity.UserId = model.UserId;
            entity.Date = model.Date;
			entity.OperatorId = model.OperatorId;
			entity.ModifyOn = model.ModifyOn;

            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }
		#endregion
    }
}
